#pragma once
#pragma warning(disable:4996)       // disable checked iterator errors http://msdn.microsoft.com/en-us/library/aa985965(VS.80).aspx 

//
// Copyright (C) 2011 - 2013 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1

#include <Macros.h>
#include <ValueHelpers.h>
#include <Settings.h>

#include <gen/QL/Instrument.h>
#pragma unmanaged 
#include <ql\instruments\creditdefaultswap.hpp>
#include <boost/smart_ptr/detail/spinlock.hpp>
#pragma managed 

using namespace System;
using namespace QuantLib;
using namespace Cephei;
using namespace Cephei::Core;
using namespace PLATFORM::Collections;

using namespace Cephei::QL;
using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL::Times;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
namespace Cephei { namespace QL { namespace Instruments {
	//////////////////////////////////////////////////////////////////////////////////////////////
	// implementation of ICreditDefaultSwap
	public ref class CCreditDefaultSwap  : 
            public CInstrument,
            public Cephei::QL::Instruments::ICreditDefaultSwap
	{
	protected: 
		boost::shared_ptr<QuantLib::CreditDefaultSwap>* _ppCreditDefaultSwap;
#ifdef HANDLE
		QuantLib::Handle<QuantLib::CreditDefaultSwap>* _phCreditDefaultSwap;
#endif
		Object^ _CreditDefaultSwapOwner;     // reference to object that manages the storage for this object
	internal:
		CCreditDefaultSwap (QL::Protection::SideEnum side, Double notional, Double upfront, Double spread, Cephei::QL::Times::ISchedule^ schedule, QL::Times::BusinessDayConventionEnum paymentConvention, Cephei::QL::Times::IDayCounter^ dayCounter, Microsoft::FSharp::Core::FSharpOption<Boolean>^ settlesAccrual, Microsoft::FSharp::Core::FSharpOption<Boolean>^ paysAtDefaultTime, Microsoft::FSharp::Core::FSharpOption<DateTime>^ protectionStart, Microsoft::FSharp::Core::FSharpOption<DateTime>^ upfrontDate, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Instruments::IClaim^>^ claim, Cephei::QL::IPricingEngine^ QL_Pricer);
		CCreditDefaultSwap (QL::Protection::SideEnum side, Double notional, Double spread, Cephei::QL::Times::ISchedule^ schedule, QL::Times::BusinessDayConventionEnum paymentConvention, Cephei::QL::Times::IDayCounter^ dayCounter, Microsoft::FSharp::Core::FSharpOption<Boolean>^ settlesAccrual, Microsoft::FSharp::Core::FSharpOption<Boolean>^ paysAtDefaultTime, Microsoft::FSharp::Core::FSharpOption<DateTime>^ protectionStart, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Instruments::IClaim^>^ claim, Cephei::QL::IPricingEngine^ QL_Pricer);
        CCreditDefaultSwap (boost::shared_ptr<QuantLib::CreditDefaultSwap>& childNative, Object^ owner);
        CCreditDefaultSwap (QuantLib::CreditDefaultSwap& childNative, Object^ owner);
        CCreditDefaultSwap (CCreditDefaultSwap^ copy);
        CCreditDefaultSwap (PLATFORM::Type^ t);
#ifdef STRUCT
        CCreditDefaultSwap (QuantLib::CreditDefaultSwap childNative);
#endif       
#ifdef HANDLE
		CCreditDefaultSwap (QuantLib::Handle<QuantLib::CreditDefaultSwap>& childNative, Object^ owner);
		CCreditDefaultSwap (QuantLib::Handle<QuantLib::CreditDefaultSwap> childNative);
#endif
		virtual ~CCreditDefaultSwap ();
		!CCreditDefaultSwap ();

	internal:
		QuantLib::CreditDefaultSwap& GetReference ();
		boost::shared_ptr<QuantLib::CreditDefaultSwap>& GetShared ();
		QuantLib::CreditDefaultSwap* GetPointer ();
        void SetCreditDefaultSwap (boost::shared_ptr<QuantLib::CreditDefaultSwap> native)
        {
            if (_ppCreditDefaultSwap != NULL)
                delete _ppCreditDefaultSwap;
            _ppCreditDefaultSwap = new boost::shared_ptr<QuantLib::CreditDefaultSwap> (native);
            SetInstrument (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppCreditDefaultSwap));
        }
#ifdef HANDLE
		QuantLib::Handle<QuantLib::CreditDefaultSwap>& GetHandle ();
#endif
		virtual bool HasNative () override;
    public:
        property Double CouponLegBPS 
        {
		    virtual Double get () ;
        }
        property Double CouponLegNPV 
        {
		    virtual Double get () ;
        }
        property Cephei::Core::IVector<Cephei::QL::ICashFlow^>^ Coupons 
        {
		    virtual Cephei::Core::IVector<Cephei::QL::ICashFlow^>^ get () ;
        }
        property Double DefaultLegNPV 
        {
		    virtual Double get () ;
        }
        property Double FairSpread 
        {
		    virtual Double get () ;
        }
		virtual Double ImpliedHazardRate (Double targetNPV, Cephei::QL::Termstructures::IYieldTermStructure^ discountCurve, Cephei::QL::Times::IDayCounter^ dayCounter, Microsoft::FSharp::Core::FSharpOption<Double>^ recoveryRate, Microsoft::FSharp::Core::FSharpOption<Double>^ accuracy) ;
		virtual Double ConventionalSpread (Double conventionalRecovery, Cephei::QL::Termstructures::IYieldTermStructure^ discountCurve, Cephei::QL::Times::IDayCounter^ dayCounter) ;
        property Boolean IsExpired 
        {
		    virtual Boolean get () ;
        }
        property Double Notional 
        {
		    virtual Double get () ;
        }
        property Boolean PaysAtDefaultTime 
        {
		    virtual Boolean get () ;
        }
        property Boolean SettlesAccrual 
        {
		    virtual Boolean get () ;
        }
        property QL::Protection::SideEnum Side 
        {
		    virtual QL::Protection::SideEnum get () ;
        }
        property Double FairUpfront 
        {
		    virtual Double get () ;
        }
        property DateTime ProtectionEndDate 
        {
		    virtual DateTime get () ;
        }
        property DateTime ProtectionStartDate 
        {
		    virtual DateTime get () ;
        }
        property Double RunningSpread 
        {
		    virtual Double get () ;
        }
        property Microsoft::FSharp::Core::FSharpOption<Double>^ Upfront 
        {
		    virtual Microsoft::FSharp::Core::FSharpOption<Double>^ get () ;
        }
        property Double UpfrontBPS 
        {
		    virtual Double get () ;
        }
        property Double UpfrontNPV 
        {
		    virtual Double get () ;
        }
    };
	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Factory class
//z	[FactoryFor(Core::Generic::ICoCell<Cephei::QL::Instruments::ICreditDefaultSwap^>::typeid)]
	[FactoryFor(Cephei::QL::Instruments::ICreditDefaultSwap::typeid)]
	[FactoryFor(Cephei::QL::Instruments::ICreditDefaultSwap_Factory::typeid)]
	public ref class CCreditDefaultSwap_Factory sealed : public ICreditDefaultSwap_Factory
	{
	public:
        virtual ICreditDefaultSwap^ Create (QL::Protection::SideEnum side, Double notional, Double upfront, Double spread, Cephei::QL::Times::ISchedule^ schedule, QL::Times::BusinessDayConventionEnum paymentConvention, Cephei::QL::Times::IDayCounter^ dayCounter, Microsoft::FSharp::Core::FSharpOption<Boolean>^ settlesAccrual, Microsoft::FSharp::Core::FSharpOption<Boolean>^ paysAtDefaultTime, Microsoft::FSharp::Core::FSharpOption<DateTime>^ protectionStart, Microsoft::FSharp::Core::FSharpOption<DateTime>^ upfrontDate, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Instruments::IClaim^>^ claim, Cephei::QL::IPricingEngine^ QL_Pricer);
        virtual ICreditDefaultSwap^ Create (QL::Protection::SideEnum side, Double notional, Double spread, Cephei::QL::Times::ISchedule^ schedule, QL::Times::BusinessDayConventionEnum paymentConvention, Cephei::QL::Times::IDayCounter^ dayCounter, Microsoft::FSharp::Core::FSharpOption<Boolean>^ settlesAccrual, Microsoft::FSharp::Core::FSharpOption<Boolean>^ paysAtDefaultTime, Microsoft::FSharp::Core::FSharpOption<DateTime>^ protectionStart, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Instruments::IClaim^>^ claim, Cephei::QL::IPricingEngine^ QL_Pricer);
    };
   
/*Cephei*/ } /*QL*/ } /*Instruments */}
